<?php
namespace app\index\controller;

use app\common\entity\Config;
use app\common\entity\MywalletLog;
use app\common\entity\User;
use app\common\shop\CartModel;
use app\common\shop\ShopGoodsModel;
use app\common\shop\SignInLogModel;
use app\common\shop\SpecListModel;
use app\common\shop\SpecModel;
use think\Db;
use think\Request;
use think\Controller;
// 购物车
class Signin extends Base {
    /**
     * [签到]
     * @param
     * @return
     */
    public function sign( Request $request ){
        $uid = $this->userId;
        // 今日是否已经签到
        $res = SignInLogModel::checkTodaySignIn($uid);
        if($res) return json(['code' => 1, 'msg' => '今日已签到']);
        $sign_in_bless = Config::getValue('sign_in_bless');// 签到等到的福币
        MywalletLog::addLog($uid, $sign_in_bless, 1 , '签到', 5, 1);
        // 添加签到记录
        SignInLogModel::add(['uid'=>$uid,'number'=>$sign_in_bless]);
        // 累计签到福币
        SignInLogModel::addSignBless($uid,$sign_in_bless);
        return json(['code' => 0, 'msg'=>'签到成功','info'=>['sign_in_bless'=>$sign_in_bless]]);
    }

    /**
     * [签到记录]
     * @param
     * @return
     */
    public function signLog( Request $request ){
        $uid = $this->userId;
        // 星期一到星期天 1、2、3、4、5、6、0。星期天为0
        $weekTime = date("w");
        if($weekTime == 0){
            $weekTime = 7;
        }
        $dayTime = 86400;// 一天的秒数
        // 今天时间
        $day = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $startTime = $day - $dayTime*($weekTime-1);// 星期一的时间

        $todaydisplay = 0;// 今天是否已经签到 0未签到
        $allSign = 0;// 已经签到天数
        $list = [];
        $week = (new SignInLogModel())->week;// 每周
        for ($i=1;$i <=7;$i++){
            $data = $week[$i-1];
            // 时间
            $time = $startTime + $dayTime*($i-1);
            $endtime = mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
            $res = SignInLogModel::checkTimeSignIn($uid,$time,$endtime);
            if($res){
                if($i == $weekTime){
                    $todaydisplay = 1;
                }
                $data['display'] = 1;
                $allSign++;
            }
            $list[] = $data;
        }
        $sumSignBless = User::where('id',$uid)->value('signbless');
        return json(['code' => 0, 'msg'=>'请求成功','info'=>['list'=>$list,'todaydisplay'=>$todaydisplay,'sumSignBless'=>$sumSignBless,'allSign'=>$allSign]]);
    }
}